<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('blogInfo:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="博客标题">
                <a-input
                  v-model="queryParam.blogTitle"
                  allow-clear
                  placeholder="请输入博客标题"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="发布状态">
                <a-select
                  style="width: 100%"
                  v-model="queryParam.blogStatus"
                  placeholder="请选择发布状态">
                  <a-select-option
                    v-for="(item,index) in blogStatusData"
                    :key="index"
                    :value="item.code">{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        size="small"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.blogId"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template class="table-operator" slot="operator" v-if="hasPerm('blogInfo:add')">
          <a-button type="primary" v-if="hasPerm('blogInfo:add')" icon="plus" @click="$refs.addForm.add()">
            新增博客信息表
          </a-button>
          <a-button type="danger" v-if="hasPerm('blogInfo:delete')" icon="delete" @click="deletePageCache()">
            删除分页（前台）缓存
          </a-button>
        </template>

        <span slot="blogStatusscopedSlots" slot-scope="text,record">
          <a-switch
            v-if="hasPerm('blogInfo:toggle:blogStatus')"
            checked-children="发布"
            un-checked-children="草稿"
            :checked="text === 1"
            @click="() => blogStatusToggle(record)"
          />
        </span>
        <span slot="isRecommendSlots" slot-scope="text,record">
          <a-switch
            :checked="text===1"
            @click="() => isRecommendToggle(record)"
          >
            <span slot="checkedChildren" ><a-icon type="fire" theme="filled" style="color: red"/>推荐</span>
            <span slot="unCheckedChildren">普通</span>
          </a-switch>
        </span>
        <span slot="isTopSlots" slot-scope="text,record">
          <a-switch
            :checked="text===1"
            @click="() => isTopToggle(record)"
          >
            <span slot="checkedChildren"><a-icon type="to-top" />置顶</span>
            <span slot="unCheckedChildren">普通</span>
          </a-switch>
        </span>
        <span slot="enableCommentscopedSlots" slot-scope="text,record">
          <a-switch
            v-if="hasPerm('blogInfo:toggle:enableComment')"
            checked-children="允许评论"
            un-checked-children="不允许评论"
            :checked="text===0"
            @click="() => enableCommentToggle(record)"
          />
        </span>
        <span slot="isDeletedscopedSlots" slot-scope="text,record">
          <a-switch
            v-if="hasPerm('blogInfo:toggle:isDeleted')"
            checked-children="正常"
            un-checked-children="删除"
            :checked="text===0"
            @click="() => isDeletedToggle(record)"
          />
        </span>

        <span slot="action" slot-scope="text, record">

          <a v-if="hasPerm('blogInfo:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('blogInfo:edit') & hasPerm('blogInfo:delete')"/>
          <a-popconfirm
            v-if="hasPerm('blogInfo:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => blogInfoDelete(record)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('blogInfo:edit') & hasPerm('blogInfo:delete')"/>
          <a-popconfirm
            v-if="hasPerm('blogInfo:delete')"
            placement="topRight"
            title="确认清空缓存？"
            @confirm="() => deleteArticleCache(record)">
            <a>清空缓存</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script>import { STable } from '@/components'
import addForm from '@/views/main/bloginfo/addForm'
import editForm from '@/views/main/bloginfo/editForm'

import {
  blogInfoDelete,
  blogInfoPage,
  blogStatusToggle, deleteArticleCache, deletePageCache,
  enableCommentToggle,
  isDeletedToggle,
  isRecommendToggle,
  isTopToggle
} from '@/api/modular/main/bloginfo/blogInfoManage'
import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'

export default {
  components: {
    STable,
    addForm,
    editForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '博客标题',
          align: 'center',
          dataIndex: 'blogTitle'
        },
       /* {
          title: '博客自定义路径url',
          align: 'center',
          dataIndex: 'blogSubUrl'
        }, */
        {
          title: '博客分类',
          align: 'center',
          dataIndex: 'blogCategoryName'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'blogStatus',
          scopedSlots: { customRender: 'blogStatusscopedSlots' }
        },
        {
          title: '阅读量',
          align: 'center',
          dataIndex: 'blogViews'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'updateTime'
        },
        {
          title: '是否允许评论',
          align: 'center',
          dataIndex: 'enableComment',
          scopedSlots: { customRender: 'enableCommentscopedSlots' }
        },
        {
          title: '是否删除',
          align: 'center',
          dataIndex: 'isDeleted',
          scopedSlots: { customRender: 'isDeletedscopedSlots' }
        },
        {
          title: '推荐状态',
          align: 'center',
          dataIndex: 'isRecommend',
          scopedSlots: { customRender: 'isRecommendSlots' }
        },
        {
          title: '置顶状态',
          align: 'center',
          dataIndex: 'isTop',
          scopedSlots: { customRender: 'isTopSlots' }
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return blogInfoPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      blogStatusData: []
    }
  },
  created() {
    if (this.hasPerm('blogInfo:edit') || this.hasPerm('blogInfo:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    const blogStatusOption = this.$options
    this.blogStatusData = blogStatusOption.filters['dictData']('dict_blog_status')
  },
  methods: {
    blogInfoDelete(record) {
      blogInfoDelete(record).then((res) => {
        if (res.success) {
          this.deleteArticleCache(record)
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败,' + res.message) //
        }
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    /**
     * 获取字典数据
     */
    sysDictTypeDropDown(text) {
      sysDictTypeDropDown({ code: 'sex' }).then((res) => {
        this.sexDictTypeDropDown = res.data
      })
      sysDictTypeDropDown({ code: 'common_status' }).then((res) => {
        this.statusDictTypeDropDown = res.data
      })
    },
    blogStatusToggle(record) {
      blogStatusToggle({ 'blogId': record.blogId }).then((res) => {
          if (res.success) {
            this.$message.success('切换成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('切换失败,' + res.message) //
          }
        }
      )
    },
    enableCommentToggle(record) {
      enableCommentToggle({ 'blogId': record.blogId }).then((res) => {
          if (res.success) {
            this.$message.success('切换成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('切换失败,' + res.message) //
          }
        }
      )
    },
    isDeletedToggle(record) {
      isDeletedToggle({ 'blogId': record.blogId }).then((res) => {
          if (res.success) {
            this.$message.success('切换成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('切换失败,' + res.message) //
          }
        }
      )
    },
    isTopToggle(record) {
      isTopToggle({ 'blogId': record.blogId }).then((res) => {
          if (res.success) {
            this.$message.success('切换成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('切换失败,' + res.message) //
          }
        }
      )
    },
    isRecommendToggle(record) {
      isRecommendToggle({ 'blogId': record.blogId }).then((res) => {
          if (res.success) {
            this.$message.success('切换成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('切换失败,' + res.message) //
          }
        }
      )
    },
    deletePageCache() {
      deletePageCache().then((res) => {
          if (res.success) {
            this.$message.success('删除缓存成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除缓存失败,' + res.message) //
          }
        }
      )
    },
    deleteArticleCache(record) {
      deleteArticleCache(record).then((res) => {
          if (res.success) {
            this.$message.success('删除缓存成功!')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除缓存失败,' + res.message) //
          }
        }
      )
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
.ant-table-row-cell-break-word{
  width: 11%;
}
</style>
